﻿using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNASpriteLib;

namespace XNASpriteLibVisualTest
{
	class SimpleSprite
	{
		static Sprite sprite;
		SpriteAnimator animator;

		private Vector2 center;
		private float angle = 0;
		private float angleStep;
		private float angleDiff = 0;
		private Vector2 offset = new Vector2(0, 0);
		private bool wDown, sDown, aDown, dDown;


		public SimpleSprite(Vector2 location)
		{
			center = location;
			//animator = new Sprite(File.OpenRead("..\\..\\sprites\\SimpleStan.sprx"), texture);
		}


		public void LoadContent(GraphicsDevice graphicsDevice)
		{
			//*
			if(sprite == null)
			{
				//sprite = new Sprite(File.OpenRead(@"Spaceship128.sprx"), graphicsDevice);
				sprite = new Sprite(File.OpenRead(@"SolarStationLevel3 128.sprx"), graphicsDevice);
			}
			animator = new SpriteAnimator(sprite);
			angleStep = 360.0f / 128.0f;
			//*/

			/*
			animator = new Sprite(File.OpenRead(@"..\..\..\sprites\Spaceship64.sprx"), texture);
			angleStep = 360.0f / 64.0f;
			//*/

			/*
			animator = new Sprite(File.OpenRead(@"Stan.sprx"), texture);
			animator.CurrentOrientation = "SE";
			animator.CurrentSet = "Sword";
			animator.CurrentAnimation = "looking";
			//*/
		}


		public void Update(TimeSpan elapsedGameTime)
		{
			angle += (float)(10 * elapsedGameTime.TotalSeconds);

			float orientation = (int)((angle + (angleStep / 2)) / angleStep) * angleStep;

			while (angle < 0f)
			{
				angle += 360f;
			}
			while(angle >= 360f)
			{
				angle -= 360f;
			}

			while (orientation >= 360f)
			{
				orientation -= 360f;
			}
			while (orientation < 0f)
			{
				orientation += 360f;
			}

			animator.CurrentOrientation = orientation.ToString();
			animator.Update(elapsedGameTime);

			//*
			float degreeAngleDiff = angle - orientation;
			angleDiff = MathHelper.ToRadians(degreeAngleDiff);// *10;
			/*/
			angleDiff = angle;
			//*/

			KeyboardState keyboardState = Keyboard.GetState();
			if(keyboardState[Keys.W] == KeyState.Down)
			{
				if(!wDown)
				{
					wDown = true;
					offset.Y += 1;
				}
			}
			else
			{
				wDown = false;
			}

			if (keyboardState[Keys.S] == KeyState.Down)
			{
				if (!sDown)
				{
					sDown = true;
					offset.Y -= 1;
				}
			}
			else
			{
				sDown = false;
			}

			if (keyboardState[Keys.A] == KeyState.Down)
			{
				if (!aDown)
				{
					aDown = true;
					offset.X -= 1;
				}
			}
			else
			{
				aDown = false;
			}

			if (keyboardState[Keys.D] == KeyState.Down)
			{
				if (!dDown)
				{
					dDown = true;
					offset.X += 1;
				}
			}
			else
			{
				dDown = false;
			}
		}

		public void Draw(SpriteBatch spriteBatch, TimeSpan timeSpan)
		{
			/*
			animator.Draw(spriteBatch, center - new Vector2(100, 100));
			/*/
			spriteBatch.Draw(animator.FrameTexture, center, animator.FrameRect, Color.White, angleDiff, animator.FrameOffset + offset, 1, SpriteEffects.None, 0);
			//spriteBatch.Draw(animator.FrameTexture, center - animator.FrameOffset, animator.FrameRect, Color.White);
			//*/

			//spriteBatch.DrawLine(center, 100, MathHelper.ToRadians(angle - 90f), Color.Red);

		}
	}
}
